<?php
 /*
  *总后台订单模型类
  */
namespace Home\Model; 
use Think\Model;
class OrderModel extends Model
{
	/*
	 *晒选订单
	 *@param  page 页面
	 *@param  filter 过滤条件
	 */
	public function index()
	{
		$ordersn=I('post.ordersn','','strip_tags');
		$areatype=I('post.areatype','','strip_tags'); //平台订单，运营商订单
		$areatype=intval($areatype);
		$paytype=I('post.paytype','','strip_tags');
		$paytype=intval($paytype);
		$starttime=I('post.starttime','','strip_tags');
		$endtime=I('post.endtime','','strip_tags');
		$orderstatus=I('post.orderstatus','','strip_tags');
		$page=I('post.page',1,'strip_tags');
		$pagesize=!empty($_COOKIE['pagesize'])?$_COOKIE['pagesize']:C('PAGESIZE');
		$shopid=I('post.shopid',0,'strip_tags');
		$condition='1=1 ';
		if(!empty($ordersn))
		{
			$condition.=" AND a.order_sn like '%$ordersn%' ";
		}
		if($areatype !=99 && !empty($areatype))
		{
			$condition.=' AND a.areatype= '.$areatype;
		}
		if($paytype !=99 && !empty($paytype))
		{
			$condition.=' AND a.pay_type= '.$paytype;
		}
		if(!empty($starttime) && !empty($endtime))
		{	$starttime=strtotime($starttime);
			$endtime=strtotime($endtime);
			$condition.=' AND a.create_time BETWEEN '.$starttime .' AND '.$endtime ;
		}
		//订单状态
		if($orderstatus !=99 && !empty($orderstatus))
		{	$orderstatus=intval($orderstatus);
			$condition.=" AND (a.shipping_status+a.pay_status+a.order_status)=$orderstatus " ;
		}
		//加盟商家
		if(!empty($shopid))
		{
			$condition.=' AND  a.shopid='.$shopid;
		}
		//符合条件的总条数
		$order=M('order a');


		$count=$order->query("select count(*) as count from ".C(DB_PREFIX).'order a where '.$condition);


		//$pagearr= getPageArr($page,C('PAGESIZE'),$count[0]['count']);
		$pagearr= getPageArr($page,$pagesize,$count[0]['count']);
		$start=$pagearr['start'];
		$pagesize=$pagearr['pagesize'];
		$sql='select a.orderid,a.order_sn,b.number,b.price,b.supperprice,(a.order_status+a.pay_status+a.shipping_status)as allstatus ,a.pay_type,a.areatype,a.shopid,FROM_UNIXTIME(a.create_time, "%Y-%m-%d %H:%i:%S") as create_time,FROM_UNIXTIME(a.pay_time, "%Y-%m-%d %H:%i:%S") as pay_time,FROM_UNIXTIME(a.fatime, "%Y-%m-%d %H:%i:%S") fatime,FROM_UNIXTIME(a.shoutime, "%Y-%m-%d %H:%i:%S") shoutime from '.C(DB_PREFIX).'order a inner join '.C(DB_PREFIX).'order_item b  
		      on a.orderid=b.orderid  where '.$condition. " order by a.orderid desc limit  $start , $pagesize";
		$orders=M()->query($sql);
		//获取经销商
		if(!empty($orders))
		{ 
			$shopuser=M('shop_user');
			foreach ($orders as $key => &$value)
			{
				$value['companyname']=$shopuser->where('shopid=%d',$value['shopid'])->getField('companyname');
				switch ($value['pay_type']) {
					case '0':
						$value['pay_type']='银行转账';
						break;
					case '1':
						$value['pay_type']='支付宝';
						break;
					
					
				}
				switch ($value['areatype'])
				{
					case '1':
						$value['area']='现货区';
						break;
					case '2':
						$value['area']='网购区';
						break;
					case '3':
						$value['area']='店长推荐区';
						break;
					case '4':
						$value['area']='乡镇专供区';
						break;
					default:
						$value['area']='网购区';
						break;
				}
				
				switch ($value['allstatus']) {
					case '0':
						$value['allstatus']='未确认';
						break;
					case '1':
						//$value['allstatus']='已确认,未付款';
						$value['allstatus']='未付款';
						break;
					case '3':
						//$value['allstatus']='已确认,已付款';
						$value['allstatus']='已付款';
						break;
					case '6':
						//$value['allstatus']='已确认,已付款,配货中';
						$value['allstatus']='配货中';
						break;
					case '8':
						//$value['allstatus']='已确认,已付款,发货中';
						$value['allstatus']='发货中';
						break;
					case '9':
						$value['allstatus']='已收货';
						break;
					case '4':
						$value['allstatus']='退货';
						break;
					case '2':
						$value['allstatus']='取消';
						break;
	
				}

			}
		}	
		$result=array();
		$result['pagearr']=$pagearr;
		$result['orders']=$orders;
		return $result;
	}


	/*
	 * 更改订单状态
	 * @param  orderid
	 * return false
	 */
	public function editStatus($arg)
	{
		
		$orderid=!empty($arg['orderid'])?$arg['orderid']:0;
		$status=!empty($arg['status'])?$arg['status']:0;
		$uname=!empty($_SESSION['uname'])?$_SESSION['uname']:'';
		$order=M('order');
		$orderAction=M('order_action');
		$order->startTrans();
		$order->create();
		$orderAction->create();
		//修改订单状态
		switch ($status)
		{
			case 2:
				//将订单状态修改为取消
				$order->order_status=2;
				$order->shipping_status=0;
				$order->pay_status=0;
				$result1=$order->where('orderid=%d',$orderid)->save();

				//修改订单记录操作表
				$orderAction->order_status=2;
				$orderAction->shipping_status=0;
				$orderAction->pay_status=0;
				$orderAction->edittime=time();
				$orderAction->action_user=$uname;
				$orderAction->order_id=$orderid;
				$result2=$orderAction->add();
				if ($result1 !==false || $result2 !== false) {
					
					$order->commit(); //成功提交
					return  true;
				}else{
					$order->rollback(); //不成功回滚
					return false;
				}
				break;
			case 1:
				//将订单状态修改为确认
				$order->order_status=1;
				$order->shipping_status=0;
				$order->pay_status=0;
				$result1=$order->where('orderid=%d',$orderid)->save();

				//修改订单记录操作表
				$orderAction->order_status=1;
				$orderAction->shipping_status=0;
				$orderAction->pay_status=0;
				$orderAction->edittime=time();
				$orderAction->action_user=$uname;
				$orderAction->order_id=$orderid;
				$result2=$orderAction->add();
				if ($result1 !==false || $result2 !== false) {
					
					$order->commit(); //成功提交
					return  true;
				}else{
					$order->rollback(); //不成功回滚
					return false;
				}
				break;
			case 3:
				//将订单状态修改为取消
				$order->order_status=1;
				$order->shipping_status=0;
				$order->pay_status=2;
				$result1=$order->where('orderid=%d',$orderid)->save();

				//修改订单记录操作表
				$orderAction->order_status=1;
				$orderAction->shipping_status=0;
				$orderAction->pay_status=2;
				$orderAction->edittime=time();
				$orderAction->action_user=$uname;
				$orderAction->order_id=$orderid;
				$result2=$orderAction->add();
				if ($result1 !==false || $result2 !== false) {
					
					$order->commit(); //成功提交
					return  true;
				}else{
					$order->rollback(); //不成功回滚
					return false;
				}
				break;
				case 6:
				//将订单状态修改为取消
				$order->order_status=1;
				$order->shipping_status=3;
				$order->pay_status=2;
				$result1=$order->where('orderid=%d',$orderid)->save();

				//修改订单记录操作表
				$orderAction->order_status=1;
				$orderAction->shipping_status=3;
				$orderAction->pay_status=2;
				$orderAction->edittime=time();
				$orderAction->action_user=$uname;
				$orderAction->order_id=$orderid;
				$result2=$orderAction->add();
				if ($result1 !==false || $result2 !== false) {
					
					$order->commit(); //成功提交
					return  true;
				}else{
					$order->rollback(); //不成功回滚
					return false;
				}
				break;

		}


		
	}
	/*
	 * 订单操作记录
	 * @param orderid
	 * return array
	 */
	public function orderLog($orderid=0)
	{
		$orderid=!empty($orderid)?$orderid:0;
		if(empty($orderid)){
			return '';
		}
		$orderAction=M('order_action');
		$logs=$orderAction->where('order_id=%d',$orderid)

		                  ->getField('actionid,order_id,action_user,(order_status+shipping_status+pay_status) as status ,action_note,FROM_UNIXTIME(edittime, "%Y-%m-%d %H:%i:%S") as edittime');
		                  
		if (!empty($logs)) {
			return $logs;
		}else{
			return '';
		}
	}


		
}